package factiva;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import util.WebDriverFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zhongwei on 2016/12/25.
 */
public class Entry {
    static List<String> weeks;
    private static String searchStr1 = "(ns=c151) and rst=tmnbus not (rst=amb or rst=ambo or rst=b or rst=wcbbe or rst=bon or rst=wc93177 or rst=wcbw or rst=wc57216 or rst=wc43649 or rst=wc88870 or rst=djdn or rst=fb or rst=wc90057 or rst=mrkwc or rst=wcmwb or rst=wsjo or rst=j or rst=wcwsjb or rst=ctgdis or rst=rstprocb or rst=ctgfvc)";
    private static String searchStr2 = "(ns=c151) and (rst=amb or rst=ambo or rst=b or rst=wcbbe or rst=bon or rst=wc93177 or rst=wcbw or rst=wc57216 or rst=wc43649 or rst=wc88870 or rst=djdn or rst=fb or rst=wc90057 or rst=mrkwc or rst=wcmwb or rst=wsjo or rst=j or rst=wcwsjb or rst=ctgdis or rst=rstprocb or rst=ctgfvc)";

    static {
        try {
            weeks = FileUtils.readLines(new File(ClassLoader.getSystemResource("weeks.txt").toURI()));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        WebDriver webDriver = WebDriverFactory.getWebDriver(null);
        login(webDriver);
        FileWriter fileWriter = new FileWriter("result_2_1.txt", true);
        Set<String> doneWeeks = new HashSet<>();
        List<String> list = FileUtils.readLines(new File("result_2_1.txt"));
        for (String str : list) {
            doneWeeks.add(str.split("\t")[0]);
        }
        for (String str : weeks) {
            if (doneWeeks.contains(str)) continue;
            System.out.println(str);
            String[] arr1 = str.split("/");
            String[] arr2 = arr1[0].split("-");
            String[] arr3 = arr1[1].split("-");
            String result = "";
            tryClear(webDriver);
            webDriver.findElement(By.id("frd")).sendKeys(arr2[2]);
            webDriver.findElement(By.id("frm")).clear();
            webDriver.findElement(By.id("frm")).sendKeys(arr2[1]);
            webDriver.findElement(By.id("fry")).clear();
            webDriver.findElement(By.id("fry")).sendKeys(arr2[0]);
            webDriver.findElement(By.id("tod")).clear();
            webDriver.findElement(By.id("tod")).sendKeys(arr3[2]);
            webDriver.findElement(By.id("tom")).clear();
            webDriver.findElement(By.id("tom")).sendKeys(arr3[1]);
            webDriver.findElement(By.id("toy")).clear();
            webDriver.findElement(By.id("toy")).sendKeys(arr3[0]);
            webDriver.findElement(By.id("ftx")).clear();
            webDriver.findElement(By.id("ftx")).sendKeys(searchStr2);
            webDriver.findElement(By.id("btnSBSearch")).click();
            while (true) {
                try {
                    result = webDriver.findElement(By.className("ctTab")).getText();
                    if (result.contains("道琼斯"))
                        break;
                } catch (Exception ex) {

                }
            }
            fileWriter.write(str + "\t" + result + "\n");
            fileWriter.flush();
            webDriver.navigate().back();
        }
        fileWriter.flush();
        fileWriter.close();
    }

    public static void tryClear(WebDriver webDriver) {
        try {
            webDriver.findElement(By.id("frd")).clear();
        } catch (Exception ex) {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tryClear(webDriver);
        }
    }

    public static void login(WebDriver webDriver) throws InterruptedException {
        webDriver.get("http://www.hec.edu/Library");
        WebElement tabs = webDriver.findElement(By.id("content-tabs"));
        WebElement tab = tabs.findElements(By.id("content-tab")).get(2);
        tab.findElement(By.id("search-text")).sendKeys("Factiva");
        tab.findElement(By.id("submit-button")).click();
        Thread.currentThread().sleep(3000);
        while (true) {
            try {
                webDriver.findElements(By.className("goto")).get(0).click();
                break;
            } catch (Exception ex) {
                Thread.currentThread().sleep(2000);
                // ignore
            }
        }
        while (true) {
            try {
                webDriver.findElement(By.name("user")).sendKeys("wu");
                break;
            } catch (Exception ex) {
                Thread.currentThread().sleep(2000);
                // ignore
            }
        }
        webDriver.findElement(By.name("pass")).sendKeys("Iloveluoluo1");
        List<WebElement> inputs = webDriver.findElement(By.id("formulaire")).findElements(By.tagName("input"));
        inputs.get(inputs.size() - 1).click();
        while (true) {
            try {
                //webDriver.findElement(By.xpath("//*[@id=\"formulaire\"]/form/p[3]/input")).click();
                WebElement selectEle = webDriver.findElement(By.id("dr"));
                selectEle.click();
                Select select = new Select(selectEle);
                List<WebElement> options = select.getOptions();
                options.get(options.size() - 1).click();
                break;
            } catch (Exception ex) {
                Thread.sleep(2000);
                // ignore
            }
        }
    }
}
